#version 400

in vec3 VertexNormal;
in vec2 VertexTexCoord;
in vec3 VertexPosition;

out vec3 LightIntensity;
out vec3 Position;
out vec3 Normal;
out vec2 TexCoord;

uniform vec4 LightPosition; // Light position in eye coords.
uniform vec3 Kd; // Diffuse reflectivity
uniform vec3 Ld; // Light source intensity

uniform mat4 TransformMatrix;
uniform mat4 ModelMatrix;
uniform mat4 ViewMatrix;
uniform mat3 NormalMatrix;
uniform mat4 ProjectionMatrix;
uniform float Offset1;
uniform float Offset2;

void main()
{
	vec3 wavePosition;
	wavePosition.x = VertexPosition.x;
	wavePosition.y = VertexPosition.y;
	wavePosition.z = sin((2 * VertexPosition.x + VertexPosition.y + 2 * 3.14159) + Offset1) / 20;
	wavePosition.z += cos((VertexPosition.x + 3 * VertexPosition.y + 2 * 3.14159) + Offset1) / 20;
	wavePosition.z += sin((VertexPosition.x + 2 * VertexPosition.y + 2 * 3.14159) + Offset2) / 20;
	wavePosition.z += cos((3 * VertexPosition.x + VertexPosition.y + 2 * 3.14159) + Offset2) / 20;
	
	vec3 waveNormal;
	waveNormal.x = cos((2 * VertexPosition.x + VertexPosition.y + 2 * 3.14159) + Offset1) * 2 / 20;
	waveNormal.x -= sin((VertexPosition.x + 3 * VertexPosition.y + 2 * 3.14159) + Offset1) * 1 / 20;
	waveNormal.x += cos((VertexPosition.x + 2 * VertexPosition.y + 2 * 3.14159) + Offset2) * 1 / 20;
	waveNormal.x -= sin((3 * VertexPosition.x + VertexPosition.y + 2 * 3.14159) + Offset2) * 3 / 20;
	
	waveNormal.y = cos((2 * VertexPosition.x + VertexPosition.y + 2 * 3.14159) + Offset1) * 1 / 20;
	waveNormal.y -= sin((VertexPosition.x + 3 * VertexPosition.y + 2 * 3.14159) + Offset1) * 3 / 20;
	waveNormal.y += cos((VertexPosition.x + 2 * VertexPosition.y + 2 * 3.14159) + Offset2) * 2 / 20;
	waveNormal.y -= sin((3 * VertexPosition.x + VertexPosition.y + 2 * 3.14159) + Offset2) * 1 / 20;
	
	waveNormal.z = 1;
	
	// Convert normal and position to eye coords
	vec3 tnorm = normalize( NormalMatrix * waveNormal);
	//vec3 tnorm = normalize( NormalMatrix * normalize(wavePosition));
	vec4 eyeCoords = ViewMatrix * ModelMatrix * TransformMatrix * vec4 (wavePosition, 1.0);
	vec3 s = normalize(vec3(LightPosition - eyeCoords));
	
	// Pasamos las coordenadas de texturas a los FShaders
	TexCoord = VertexTexCoord;

	// The diffuse shading equation
	LightIntensity =  Ld * Kd * max( dot( s, tnorm ), 0.0 );
			
	// Convert position to clip coordinates and pass along
	gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * TransformMatrix * vec4(wavePosition,1.0);
}